package com.panghl.utils;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import java.util.Map;

/**
 * @Author panghl
 * @Description 导出引擎工具
 **/
public class ExcelExportEngine {

    public static Workbook writeToExcel(File rootPath, String photos, Object object, Workbook workbook) throws IOException {
        //把一个Bean转成Map
        Map<String, Object> map = EntityUtils.entityToMap(object);
        Sheet sheet = workbook.getSheetAt(0);
        Row row = null;
        Cell cell = null;
        //循环100行，每一行循环100个单元格
        for (int i = 0; i < 100; i++) {
            row = sheet.getRow(i);
            if (row == null) {
                break;
            } else {
                for (int j = 0; j < 100; j++) {
                    cell = row.getCell(j);
                    if (cell != null) {
                        writeToCell(cell,map);
                    }
                }
            }
        }

        if (rootPath!=null && photos!=null){
            //放入图片
            setImage(rootPath,photos,workbook);
            workbook.removeSheetAt(1);
        }
        return workbook;
    }

    //比较单元格的值，是否和map中的key一致，如果一致向单元格中放入map的value值
    private static void writeToCell(Cell cell, Map<String, Object> map) {
        CellType cellType = cell.getCellType();
        switch (cellType) {
            case FORMULA: //公式
                return;
        }
        String cellKey = cell.getStringCellValue();
        if (!StringUtils.isEmpty(cellKey)){
            for (String key : map.keySet()) {
                if (key.equals(cellKey)){
                    cell.setCellValue(map.get(key).toString());
                }
            }
        }
    }

    private static void setImage(File rootPath, String photo, Workbook workbook) throws IOException {
        Sheet sheet = workbook.getSheetAt(0);
        //      照片的位置
        //开始处理照片
        //先创建一个字节输出流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //读取图片   放入一个带有缓存区的图片类中
        String extName = photo.substring(photo.lastIndexOf(".")+1).toUpperCase();
        BufferedImage bufferedImage = ImageIO.read(new File(rootPath + photo));
        //把图片写入到了字节输出流中
        ImageIO.write(bufferedImage,extName,byteArrayOutputStream);
        // 创建一个绘图控制类，负责画图
        Drawing patriarch = sheet.createDrawingPatriarch();
// 指定把图片放到哪个位置
        //偏移的单位：是一个英式公制的单位  1厘米=360000

        Sheet sheet1 = workbook.getSheetAt(1);
        int col1 = ((Double)sheet1.getRow(0).getCell(0).getNumericCellValue()).intValue();
        int row1 = ((Double)sheet1.getRow(0).getCell(1).getNumericCellValue()).intValue();
        int col2 = ((Double)sheet1.getRow(0).getCell(2).getNumericCellValue()).intValue();
        int row2 = ((Double)sheet1.getRow(0).getCell(3).getNumericCellValue()).intValue();
        ClientAnchor anchor = new XSSFClientAnchor(360000, 0, 0, 0, col1, row1, col2, row2);


        int format = 0;
        switch (extName){
            case "JPG":
                format = XSSFWorkbook.PICTURE_TYPE_JPEG;
        }

// 开始把图片写入到sheet指定的位置
        patriarch.createPicture(anchor, workbook.addPicture(
                byteArrayOutputStream.toByteArray(), format));

        //处理照片结束

    }

}
